exports.GiftTableViewCell = UI.ListViewItem.subclass({
    
    className: "GiftTableViewCell",
    
    initialize: function(reuseId,cellModel,pw) {
		//$super(reuseId);
    	this._parentWindow = pw;
    	this._cellView = null;
    	this._indexPath_Row = 0;
        this._cellModel = cellModel;
        this._itemId = null;
    },
    
    _onCreateView: function() {
		this._cellView = new CustomCellView();
		
		var cellBgPath = this.getBaseResourcePath() + "cell-background.png";
		var placeholderPath = this.getBaseResourcePath() + "placeholder.png";
		var giftButtonNormalPath = this.getBaseResourcePath() + "button-normal.png";
		var giftButtonPressedPath = this.getBaseResourcePath() + "button-pressed.png";

        this._cellView.setFrame([TCUtil.getAdjustedX(0),TCUtil.getAdjustedY(0),TCUtil.getAdjustedX(280),TCUtil.getAdjustedY(90)]);
        
		this._cellView.backgroundView = UIFactory.getView([TCUtil.getAdjustedX(10),TCUtil.getAdjustedY(5),TCUtil.getAdjustedX(275),TCUtil.getAdjustedY(90)], null, null);
		this._cellView.addChild(this._cellView.backgroundView);
		
		this._cellView.cellBg = UIFactory.getImage([TCUtil.getAdjustedX(0), TCUtil.getAdjustedY(0), TCUtil.getAdjustedX(275),TCUtil.getAdjustedY(90)], cellBgPath);
		this._cellView.backgroundView.addChild(this._cellView.cellBg);

		this._cellView.giftButton = UIFactory.getGenericButton([TCUtil.getAdjustedX(180), TCUtil.getAdjustedY(50), TCUtil.getAdjustedX(70),TCUtil.getAdjustedY(30)], giftButtonNormalPath, giftButtonPressedPath, "Gift", "000000", TCUtil.getAdjustedFontSize(60), null, null);
		this._cellView.addChild(this._cellView.giftButton);		
		
        this._cellView._itemName = UIFactory.getTextArea([TCUtil.getAdjustedX(100), TCUtil.getAdjustedY(10), TCUtil.adjustWidthWithDevice(190), TCUtil.adjustHeightWithDevice(30)], "", TCUtil.getAdjustedFontSize(70), UI.ViewGeometry.Gravity.Left, 'ffcc99');
		this._cellView.addChild(this._cellView._itemName);
		
		this._cellView._itemDescription = UIFactory.getTextArea([TCUtil.getAdjustedX(100), TCUtil.getAdjustedY(30), TCUtil.adjustWidthWithDevice(170), TCUtil.adjustHeightWithDevice(30)], "", TCUtil.getAdjustedFontSize(60), UI.ViewGeometry.Gravity.Left, 'ffcc99');
		this._cellView.addChild(this._cellView._itemDescription);
		
		this._cellView._itemPrice = UIFactory.getTextArea([TCUtil.getAdjustedX(100), TCUtil.getAdjustedY(50), TCUtil.adjustWidthWithDevice(170), TCUtil.adjustHeightWithDevice(30)], "", TCUtil.getAdjustedFontSize(70), UI.ViewGeometry.Gravity.Left, 'ffcc99');
		this._cellView.addChild(this._cellView._itemPrice);		
		
//		this._cellView._BigButton = UIFactory.getGenericButton([TCUtil.getAdjustedY(10),TCUtil.getAdjustedX(5),TCUtil.getAdjustedY(275),TCUtil.getAdjustedX(90)]);
//		this._cellView._BigButton.setAlpha(0.02);
//		this._cellView.addChild(this._cellView._BigButton);

        return this._cellView;
    },
    _onSetView: function(cellView) 
    {	    	
    	this._itemId = GACommonUtils.getKeyForStoreObject(this._cellModel);
    	//console.log("zzz item TYPE is"+TCUtil.getItemType(this._cellModel));
    	
    	cellView._itemName.setText(this._cellModel.name);
    	cellView._itemDescription.setText(this._cellModel.description);
    	
    	var priceForBucket = this._cellModel.buy_prices["default"];        
        var defaultBuyPrice = TCUtil.getPrice(priceForBucket);

		var currencyType = TCUtil.getCurrency(priceForBucket);
    	if(currencyType == kCurrencyType.kCurrencyTypeNuggets)
    	{
			cellView._itemPrice.setText(defaultBuyPrice + " "+Core.Localization.getString("NUGGETS"));
    	}
    	else
    	{
    		cellView._itemPrice.setText(defaultBuyPrice + " "+Core.Localization.getString("COINS"));
    	} 
    	
		if(cellView.icon)
		{
			cellView.removeChild(cellView.icon);
			cellView.icon = null;
		}    	

		var icon = new CustomStoreIcon();
		icon.setFrame([TCUtil.getAdjustedX(39), TCUtil.getAdjustedY(27), TCUtil.getAdjustedX(45), TCUtil.getAdjustedY(45)]);
		icon.load(this._cellModel);
		cellView.icon = icon;
		cellView.addChild(cellView.icon);


//		cellView._BigButton.onclick = function() 
//        {
//            this.onCellTap(this._cellModel);
//        }.bind(this);
//        
	    cellView.giftButton.controller = this;
		cellView.giftButton.onclick = function() 
        {
            this.onGiftTap(this._cellModel);
        }.bind(this);
    },
    
    onGiftTap: function(cellModel)
    {
    	MMSoundManager.playEffect(BUTTON_TAP_SOUND);
		var priceForBucket = cellModel.buy_prices["default"];            
        var defaultBuyPrice = TCUtil.getPrice(priceForBucket);
    	var currencyType = TCUtil.getCurrency(priceForBucket);
    	var isValidPurchase = DatabaseHelper.isValidPurchase(currencyType, defaultBuyPrice);
    	
    	if(isValidPurchase)
    	{ 	
    		if(currencyType == kCurrencyType.kCurrencyTypeNuggets)
	    	{
	    		operation = kOperationType.kOperationSubtractNuggets;
	    	}
	    	else
	    	{
				operation = kOperationType.kOperationSubtractCoins;
	    	} 
	    	
	    	DatabaseHelper.updateCurrency(operation, defaultBuyPrice);
    		var itemId = GACommonUtils.getKeyForStoreObject(cellModel);
    		var senderName = DatabaseHelper.getGamerTag(); 
    		var userId = GAConfigurations.getRecordID();
    		
    		var giftJSON = {"name":senderName,"userid":userId,"itemid":itemId,"actiontype":kNeighborActionType.kNeighborActionTypeSendGift};
			MMFriendActionManager.sendGiftToFriend(JSON.stringify(giftJSON));
			
			if(DatabaseHelper.getHasCompletedTutorial() == false)
			{
				MMGameLayer.getControlLayer().getBottomBar().hideLeftTutorialIndicatorForMenu();
				MMGameLayer.getControlLayer().getBottomBar().showTutorialForHome();
			}
			
    		this._parentWindow.removeFromParent();
			this._parentWindow.destroy();
    	}
    	else 
    	{
    		//TODO zaki invalid purchase show alert...
    		var alertBox = UIFactory.getAlertBox(Core.Localization.getString("MESSAGE"), GALocalizationHandler.getRelativeString(Core.Localization.getString("NOT_ENOUGH_CASH_TO_BUY"),[cellModel.name]), ["ok"], this);
    	}
    },
    
	getBaseResourcePath: function()
	{
		return "./Content/images/gamelayer/controllayer/gifttableview/";
	},

    destroy: function() 
	{
    	
    },
});

